﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.hhx;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Domains.wbx;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.Beginning.Api.Application.Command.ScionChange
{
    public class GetScionChangeHandler : IRequestHandler<GetScionChangeCommand, Result<PageResultDto<ScionChangeDto>>>
    {
        /// <summary>
        /// 后裔转种仓储
        /// </summary>
        private readonly IBaseRepository<ScionChanges> scionChangesRepository;
        /// <summary>
        /// 猪耳信息仓储
        /// </summary>
        private readonly IBaseRepository<BasicInformation> basicInformationRepository;
        /// <summary>
        /// 栋舍信息仓储
        /// </summary>
        private readonly IBaseRepository<Bedrooms> bedroomsRepository;
        /// <summary>
        ///  栏位仓储
        /// </summary>
        private readonly IBaseRepository<Fields> fieldsRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="scionChangesRepository"></param>
        /// <param name="basicInformationRepository"></param>
        /// <param name="bedroomsRepository"></param>
        /// <param name="fieldsRepository"></param>
        public GetScionChangeHandler(IBaseRepository<ScionChanges> scionChangesRepository, IBaseRepository<BasicInformation> basicInformationRepository, IBaseRepository<Bedrooms> bedroomsRepository, IBaseRepository<Fields> fieldsRepository)
        {
            this.scionChangesRepository = scionChangesRepository;
            this.basicInformationRepository = basicInformationRepository;
            this.bedroomsRepository = bedroomsRepository;
            this.fieldsRepository = fieldsRepository;
        }

        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PageResultDto<ScionChangeDto>>> Handle(GetScionChangeCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<ScionChangeDto>>() { Code = (int)CommonErrorCode.Search, Data = new PageResultDto<ScionChangeDto>() };
            var page = new PageResultDto<ScionChangeDto>() { TotalCount = 0, PageCount = 0, List = new List<ScionChangeDto>() };
            try
            {
                var list = await(from a in (await scionChangesRepository.GetAll())
                                 join b in (await basicInformationRepository.GetAll())
                                 on a.BasicInforId equals b.BasicInforId
                                 join c in (await bedroomsRepository.GetAll())
                                 on a.BedroomId equals c.BedroomId
                                 join d in (await fieldsRepository.GetAll())
                                 on a.FieldId equals d.FieldId
                                 where a.IsScion == 0
                                 select new ScionChangeDto()
                                 {
                                     ScionChangeId = a.ScionChangeId,
                                     ScionChangeDate=a.ScionChangeDate,
                                     BasicInforId = a.BasicInforId,
                                     TurnBuilding = a.TurnBuilding,
                                     TurnColumn = a.TurnColumn,
                                     BedroomId = a.BedroomId,
                                     FieldId = a.FieldId,
                                     Gender = b.Gender,
                                     ScionDay = a.ScionDay,
                                     IsScion = a.IsScion,
                                     EarNumber = b.EarNumber,
                                     BedroomName = c.BedroomName,
                                     FieldName = d.FieldName,
                                     CreatedOwner = a.CreatedOwner,
                                     CreatedTime = a.CreatedTime,
                                     UpdateOwner = a.UpdateOwner,
                                     UpdateTime = a.UpdateTime,
                                     ExamineOwner = a.ExamineOwner,
                                     ExamineTime = a.ExamineTime,
                                 }).ToListAsync();
                //耳号查询
                if (request.BasicInforId != 0)
                {
                    list = list.Where(d => d.BasicInforId == request.BasicInforId).ToList();
                }
                //总条数
                page.TotalCount = list.Count();
                page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / request.pageSize);
                page.List = list.OrderBy(d => d.ScionChangeId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
                result.Data = page;
                    result.Code = (int)CommonErrorCode.Success;
            }
            catch (Exception)
            {
                result.Msg = "后裔转种分页列表获取异常";
            }
            return result;
        }
    }
}
